<?php

namespace common\models\activity;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%activity}}".
 *
 * @property int $id
 * @property string $name 活动
 * @property string|null $description 简介
 * @property string|null $type 活动类型
 * @property int $begin_time 开始时间
 * @property int $end_time 结束时间
 * @property int $form_id
 * @property string|null $brand 活动logo 480X310
 * @property string|null $cover 活动banner 750X310
 * @property string|null $content 活动内容
 * @property string|null $template
 * @property int $recommend_at
 * @property int $listorder
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at
 */
class Activity extends \common\db\ActiveRecord {

    use \common\traits\HandlesTrait;

    /**
     * @var integer 启用状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * @var integer 活动类型
     */
    const TYPE_SCREEN = 'screen';
    const TYPE_DRAW = 'draw';
    const TYPE_ENROLL = 'enroll';
    const TYPE_EXCHANGE = 'exchange';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%activity}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['begin_time', 'end_time', 'form_id', 'recommend_at', 'listorder', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['content'], 'string'],
            [['name', 'type', 'template'], 'string', 'max' => 50],
            [['description', 'brand', 'cover'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '活动',
            'description' => '简介',
            'type' => '活动类型',
            'begin_time' => '开始时间',
            'end_time' => '结束时间',
            'form_id' => 'Form ID',
            'brand' => '活动logo 480X310',
            'cover' => '活动banner 750X310',
            'content' => '活动内容',
            'template' => 'Template',
            'recommend_at' => 'Recommend At',
            'listorder' => 'Listorder',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => 'Deleted At',
            'enroll' => '立即报名',
            'isEnroll' => '已报名',
        ];
    }

    /**
     * 属性额外配置信息
     * @return array
     */
    public function attributeOptions() {
        return [
            'enroll' => [
                'mplink' => Url::mplink('/pages/activity/signup/main', [
                    'id' => $this->id
                ]),
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getForm() {
        return $this->hasOne(\common\models\form\Form::class, ['id' => 'form_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getEnroll() {
        return $this->hasOne(ActivityEnroll::class, ['activity_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getMineEnroll() {
        return $this->getEnroll()->andWhere([
            'user_id' => Yii::$app->user->id
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getThumb() {
        return Url::attach($this->brand);
    }

    /**
     * {@inheritdoc}
     */
    public function getMedias() {
        return [
            [
                'path' => $this->thumb,
                'format' => 'image',
            ]
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getFormat() {
        return 'activity';
    }

    /**
     * {@inheritdoc}
     */
    public function getMplink() {
        return Url::mplink('/pages/activity/view/main', [
            'id' => $this->id
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getCanEnroll() {
        if ($this->status != static::STATUS_ACTIVE) {
            return false;
        }
        if ($this->begin_time > time() || $this->end_time < time()) {
            return false;
        }
        if ($this->mineEnroll) {
            return false;
        }
        return true;
    }

    /**
     * 是否已经报名
     * @return array
     */
    public function getCanIsEnroll() {
        if ($this->mineEnroll) {
            return true;
        }
        return false;
    }

}
